package com.software.sqlite.service.impl;

import com.alibaba.fastjson.JSON;
import com.software.sqlite.api.admin.v1.vo.AdminLoginUserVO;
import com.software.sqlite.api.admin.v1.vo.AdminV1Result;
import com.software.sqlite.common.BaseMapper;
import com.software.sqlite.entity.TbMovie;
import com.software.sqlite.entity.TbMovieServer;
import com.software.sqlite.entity.YstMovieResult;
import com.software.sqlite.mapper.TbMovieMapper;
import com.software.sqlite.mapper.TbMovieServerMapper;
import com.software.sqlite.service.ITbMovieService;
import com.software.sqlite.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class TbMovieService extends BaseService<TbMovie,String,Object> implements ITbMovieService {

    @Resource
    private TbMovieMapper mapper;
    @Override
    protected BaseMapper<TbMovie> getBaseMapper() {
        return mapper;
    }
    @Value("${file.basedir}")
    private String basedir;
    @Resource
    private TaskService taskService;
    @Resource
    private TbMovieServerMapper movieServerMapper;
    @Resource
    private YstMovieService ystMovieService;


    @Override
    public AdminV1Result getPlayUrl(Long movieId) {
        AdminV1Result result=new AdminV1Result();
        Integer code=1;
        String url="";
        String msg="获取播放地址失败";
        try {
            TbMovieServer movieServer = new TbMovieServer();
            List<TbMovieServer> list = movieServerMapper.selectListBySQL(movieServer);
            String shipServerUrl = list.get(0).getServerUrl();
            Map<String, Object> regitsterDeviceParams = new LinkedHashMap<>();
            String uuid="";
            /**
             * {
             * 	"C": "Device",
             * "D": {
             * "IP": "192.168.1.1",
             * "MAC": "DD:DD:DD:DD:DD:DD",
             * "ModelNo": "BOX-002",
             * "Title": "BOX-002",
             * "UniqueCode": "b562e3d8a4314e9e9ae6c3648855b2e2",
             * "Version": 1.23
             * },
             * "F": "Regdevice",
             * "M": "DD:DD:DD:DD:DD:DD",
             * "U": "b562e3d8a4314e9e9ae6c3648855b2e2"
             * }
             */
            String uuidMac="";
            if(uuid.length()<12)
            {
                uuidMac= UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
            }else{
                uuidMac=uuid.toUpperCase();
            }
            String mac=uuidMac.substring(0,2)+":"+uuidMac.substring(2,4)+":"+uuidMac.substring(4,6)+":"+uuidMac.substring(6,8)+":"+uuidMac.substring(8,10)+":"+uuidMac.substring(10,12);
            regitsterDeviceParams.put("C", "Device");
            Map<String, Object> D = new LinkedHashMap<>();
            D.put("IP", "192.168.1.1");
            D.put("MAC", mac);
            D.put("ModelNo", "BOX-001");
            D.put("Title", "BOX-001");
            D.put("UniqueCode", uuid);
            D.put("Version", 1.23);
            regitsterDeviceParams.put("D", D);
            regitsterDeviceParams.put("F", "Regdevice");
            regitsterDeviceParams.put("M", mac);
            regitsterDeviceParams.put("U", uuid);
            String regitsterDeviceShipServerResult = ystMovieService.getRegitsterDeviceShipServer(shipServerUrl, regitsterDeviceParams);
            log.info("regitsterDeviceShipServerResult=="+regitsterDeviceShipServerResult);
            if (CommonUtils.isNotBlank(regitsterDeviceShipServerResult)) {
                Map<String, Object> getPlayUrlParams = new LinkedHashMap<>();
                getPlayUrlParams.put("mac", mac);
                getPlayUrlParams.put("filmsource", "2");
                getPlayUrlParams.put("movieId",movieId);
                String getPlayUrlResult = ystMovieService.getPlayUrlByShipServer(shipServerUrl, getPlayUrlParams);
                if (CommonUtils.isNotBlank(getPlayUrlResult)) {
                    YstMovieResult getPlayUrlData = JSON.parseObject(getPlayUrlResult, YstMovieResult.class);
                    if (getPlayUrlData != null && getPlayUrlData.isState()) {
                        Map<String,Object> getPlayUrlDataMap= (Map<String, Object>) getPlayUrlData.getData();
                        if(getPlayUrlDataMap.containsKey("Url")){
                            url=CommonUtils.parseStringData(getPlayUrlDataMap.get("Url"));
                            log.info("Url=="+url);
                            code=0;
                        }else{
                            msg="在影视服务器获取url数据解析Url失败";
                        }
                    }else{
                        String info=CommonUtils.parseStringData(getPlayUrlData.getInfo());
                        msg="在影视服务器获取url数据解析Url失败";
                        log.info("在影视服务器获取url数据解析Url失败："+getPlayUrlResult);
                        if(CommonUtils.isNotBlank(info)){
                            msg=info+"，请联系技术人员";
                        }                        }
                }else{
                    msg="在影视服务器获取url失败";
                }
            }else{
                msg="设备在影视服务器注册失败";
            }
            msg="获取播放地址成功";
        }catch (Exception e){
            e.printStackTrace();
        }
        result.setData(url);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public AdminV1Result syncMovieList()
    {
        AdminV1Result result=new AdminV1Result();
        Integer code=1;
        String msg="同步失败";
        try {
            taskService.autoSynchronyMovies();
            taskService.autoLocalDJMovies();
            taskService.autoLocalZyMovies();
//            taskService.autoSynchronyCloud();
            code=0;
            msg="同步成功";
        }catch (Exception e){
            e.printStackTrace();
        }
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    @Override
    public boolean edit(TbMovie model) {
        boolean result=false;
        try{
            if(model!=null){
                AdminLoginUserVO loginUser=this.getLoginUser(model.getToken());
                model.setUseStatus("normal");
                if(model.getMovieId()==null){
                    model.setCreateTime(new Date());
                    model.setCreateAdminId(loginUser.getUserId());
                    add(model);
                }else{
                    model.setUpdateTime(new Date());
                    model.setUpdateAdminId(loginUser.getUserId());
                    model.setCreateTime(new Date());
                    model.setCreateAdminId(loginUser.getUserId());
                    mapper.updateModel(model);
                }
                result=true;
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return result;
    }

    @Override
    public void importData(TbMovie model) throws Exception {
        log.info("importData...");
    }

    @Override
    public void exportData(TbMovie model) throws Exception {
        log.info("exportData...");
    }

}
